set.seed(123456789)

outcomes <- c("absenteeism_action", # main table 4
							 "repeat_absence", # Parents should act, table 5
							 "village_action", # Community would intervene, table 5
							 "abs_efficacy", # "Intervention is effective", table 5
							 "abs_discussion", # "Discussed absenteeism", table 6
							 "abs_goal", # "Teachers/absenteeism important", table 6
							 "more_schools", # "Schools important", table 6
							 "candidate", # "Candidate platform", table 6
							 "absent_frequency", #"Reported Absenteeism" (compliers), table 7,
							 "complain_absenteeism_el", # VHT "Parents Complain", table 7
							 "pta_frequency", # miscellaneous "PTA frequency", table D5
							 "community_state", # miscellaneous, "Community fundraiser" table D5
							 "gov_resp" # miscellaneous, "Parent’s responsibility" table D5
						)

# absenteeism_action
absenteeism_action_el_pvals <- get_RI_pvals(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")



# repeat_absence
repeat_absence_el_pvals <- get_RI_pvals(
	outcome = "repeat_absence",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# village_action
village_action_el_pvals <- get_RI_pvals(
	outcome = "village_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# abs_efficacy
abs_efficacy_el_pvals <- get_RI_pvals(
	outcome = "abs_efficacy",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# abs_discussion
abs_discussion_el_pvals <- get_RI_pvals(
	outcome = "abs_discussion",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# abs_goal
abs_goal_el_pvals <- get_RI_pvals(
	outcome = "abs_goal",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# more_schools
more_schools_el_pvals <- get_RI_pvals(
	outcome = "more_schools",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")



# candidate

candidate_pvals <- get_RI_pvals(
	outcome = "candidate",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = c("name_educ","mr_mrs_educ","non_educ_policy"), 
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# absent_frequency
absent_frequency_compliers_pvals <- get_RI_pvals(
	outcome = "absent_frequency",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"), 
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "two") #as per PAP.



# complain_absenteeism_el
vht_village_level_el <- 
	vht_el %>% 
	group_by(tc_id,block_id,absenteeism) %>% 
	summarize(
		complain_absenteeism_el = mean(complain_absenteeism,na.rm = T),
		radius = unique(radius),
		n_end_mean = unique(n_end_mean)
	)
vht_village_level_el$tc <- vht_village_level_el$tc_id


complain_absenteeism_el_pvals <- get_RI_pvals(
	outcome = "complain_absenteeism_el",
	treatment = "absenteeism",
	resample_FE = F,
	block_FE = TRUE,
	audience_size = T,
	cluster_SE = F,
	covariates = NULL,
	the_data = vht_village_level_el,
	assignment_data = treatment_assignment,
	sims = sims,
	lwr_upr_two = "two")


# pta_frequency
pta_frequency_pvals <- get_RI_pvals(
	outcome = "pta_frequency",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# community_state
community_state_pvals <- get_RI_pvals(
	outcome = "community_state",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


# gov_resp
gov_resp_pvals <- get_RI_pvals(
	outcome = "gov_resp",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


pvals <- list(absenteeism_action_el_pvals,
					 repeat_absence_el_pvals,
					 village_action_el_pvals,
					 abs_efficacy_el_pvals,
					 abs_discussion_el_pvals,
					 abs_goal_el_pvals,
					 more_schools_el_pvals,
					 candidate_pvals,
					 absent_frequency_compliers_pvals,
					 complain_absenteeism_el_pvals,
					 pta_frequency_pvals,
					 community_state_pvals,
					 gov_resp_pvals)

pvals <- sapply(pvals, function(x) x$ri_pvals["absenteeism"])


table_data <- data.frame(outcome  = c("Conative attitudes ABS",
																			"Parents should act",
																			"Community would intervene",
																			"Intervention is effective",
																			"Discussed absenteeism",
																			"Teachers/absenteeism important",
																			"Schools important",
																			"Candidate platform",
																			"Reported absenteeism",
																			"Parents complain",
																			"PTA frequency",
																			"Community fundraiser",
																			"Parent's responsibility"),
												 pvalue = pvals)


table_data <- table_data[order(table_data$pvalue),]

table_data <- table_data %>%
	mutate(rank = 1:nrow(table_data),
				 critical_val01 = round(0.1*rank/nrow(table_data), 4),
				 critical_val02 = round(0.2*rank/nrow(table_data),4),
				 critical_val03 = round(0.3*rank/nrow(table_data),4))

table_data$pvalue	<- round(table_data$pvalue, 3)			 

sink("03_tables/multiple_comparisons_BH_absenteeism.tex")
print(kable(table_data, row.names = F, format = "latex",
			col.names = c("Outcome",
										"RI $p$-value",
										"Rank",
										"Critical Value $10\\%$",
										"Critical Value $20\\%$",
										"Critical Value $30\\%$"),
			align = c("l", rep("c",5)),
			escape = F
))
sink()




